package cn.edu.scau.cmi.mazhuanglong.client;

import java.util.Scanner;



import cn.edu.scau.cmi.mazhuanglong.composite.saft.SafeOrganization;
import cn.edu.scau.cmi.mazhuanglong.composite.saft.SafePerson;
import cn.edu.scau.cmi.mazhuanglong.composite.saft.SafeTeam;

public class SafeCompositeClient {
	
	private static void addNode(SafeTeam node, String childName, String teamName, Boolean isTeam) {
		if(node.getName().equals(teamName)) {
			SafeOrganization newNode = isTeam ? new SafeTeam(childName) : new SafePerson(childName);
			node.addChild(newNode);
			return;
		}
		for(SafeOrganization child : node.getChildren()) {
			if(child instanceof SafeTeam) {
				addNode((SafeTeam)child, childName, teamName, isTeam);
			}
		}
	}
	
	
	private static void Play(SafeOrganization node, int depth) {
		for(int i=0; i<depth; i++) System.out.print(" ");
		if(node instanceof SafePerson) {
			String name = ((SafePerson)node).getName();
			System.out.println(name + ":{");
			for(int i=0; i<depth+name.length()+2; i++) System.out.print(" ");
			System.out.println("type:person");
		}
		else if(node instanceof SafeTeam) {
			String name = ((SafeTeam)node).getName();
			System.out.println(name + ":{");
			for(int i=0; i<depth+name.length()+2; i++) System.out.print(" ");
			System.out.println("type:team");
			for(int i=0; i<depth+name.length()+2; i++) System.out.print(" ");
			System.out.println("children:[");
			for(SafeOrganization child : ((SafeTeam) node).getChildren()) {
				Play(child, depth+name.length()+12);
			}
			for(int i=0; i<depth+name.length()+2; i++) System.out.print(" ");
			System.out.println("]");
		}
		for(int i=0; i<depth; i++) System.out.print(" ");
		System.out.println("}");
	}
	public static void main(String[] args) {
		SafeTeam root = new SafeTeam("Root");
		System.out.println("根节点：Root");
		String name;
		String team;
		Scanner str = new Scanner(System.in);
		while (true) {
			System.out.println("1、新增个人；2、新增团队；0、运行结束");
			int op = str.nextInt();
			if (op == 0)
				break;
			switch (op) {
			case 1: {
				System.out.println("输入个人名字");
				name = str.next();
				System.out.println("输入此人的团队");
				team = str.next();
				addNode(root, name, team, false);
				break;
			}
			case 2: {
				System.out.println("输入团队名字");
				name = str.next();
				System.out.println("输入团队的团队");
				team = str.next();
				addNode(root, name, team, true);
				break;
			}
			default:
				break;
			}
			Play(root, 0);
		}
	}
}
